<template>
	<view>
    <customtop top_title="公司认证"></customtop>
    <view class="container" v-if="!loading">
      <form class="luckform" @submit="formSubmit" v-if="user.company_auth == 0">
        <view class="items">
          <view class="item weui-cell">
            <view class="weui-cell__hd">
              <view class="weui-label">公司全称<span class="span_required">*</span></view>
            </view>
            <view class="weui-cell__bd">
              <input name="company_name" class="weui-input" type="text" placeholder="请输入公司全称" value="" />
            </view>
          </view>
          <view class="item weui-cell">
            <view class="weui-cell__hd">
              <view class="weui-label">信用代码<span class="span_required">*</span></view>
            </view>
            <view class="weui-cell__bd">
              <input name="social_credit_code" class="weui-input" type="text" placeholder="请输入信用代码" value="" />
            </view>
          </view>
        </view>
        <view class="items">
          <view class="top">
            <span class="title">营业执照</span><span class="span_remark">请上传营业执照</span>
          </view>
          <view class="weui-cell">
            <view class="weui-cell__bd">
              <view>
                <u-upload
                  :fileList="fileList_business_license"
                  @afterRead="afterRead_images"
                  @delete="delete_images"
                  multiple
                  name="business_license"
                  :maxCount="1"
                />
              </view>
            </view>
          </view>
        </view>
        <view class="btnbox">
          <button class="luckbtn luckbtn-default" formType="submit">提交信息</button>
        </view>
      </form>

      <view class="status_box" v-if="user.company_auth != 0">
        <view class="" v-if="user.company_auth == 2">
          <u-alert title="审核中,请耐心等待~" type="warning"></u-alert>
        </view>
        <view class="" v-if="user.company_auth == 3">
          <u-alert title="审核失败,请重新认证~" type="error"></u-alert>
          <view style="margin-top: 10px;"><u-alert :description="'失败原因:' + (user.company_auth_log.message ? user.company_auth_log.message : '无')" type="error"></u-alert></view>
          <button class="luckbtn luckbtn-default" style="margin-top: 30rpx;" @click="reset">重新审核</button>
        </view>
        <view class="" v-if="user.company_auth == 1">
          <u-alert title="已实名认证~" type="success"></u-alert>
        </view>
      </view>
    </view>

	</view>
</template>

<script>
import { request, upload } from "@/utils/http.js"
import customtop from "@/components/customtop.vue"

export default {
  components: { customtop },

  data() {
    return {
      loading: true,
      user: {},
      fileList_business_license: [],
      business_license: '',
    }
  },

  onLoad: function() {
    uni.showLoading();
    this.getUser();
  },

  methods: {
    getUser: function() {
      request.post('/user/getLoginUser').then(res => {
        this.loading = false;
        uni.hideLoading();
        this.user = res.data;
      })
    },

    formSubmit: function(e) {
      uni.showLoading();
      let that = this;
      let params = e.detail.value;
      params.business_license = this.business_license;
      request.post('/user/company_auth', params).then(res => {
        uni.hideLoading();
        if (res.code == 200) {
          that.getUser();
          uni.showToast({
            icon: 'none',
            title: '操作成功',
          })
        } else if (res.code == 400) {
          uni.showToast({ title: res.message, icon: 'none' });
        }
      })
    },

    reset: function() {
      uni.showLoading();
      request.post('/user/company_auth_reset').then(res => {
        uni.hideLoading();
        if (res.code == 200) {
          this.getUser();
        } else if (res.code == 400) {
          uni.showToast({ title: res.message, icon: 'none' });
        }
      })
    },

    afterRead_images: function(event) {
      let lists = [].concat(event.file);
      let fileListLen = this[`fileList_${event.name}`].length;
      lists.map((item) => {
        this[`fileList_${event.name}`].push({
          ...item,
          status: 'uploading',
          message: '上传中'
        })
      })

      for (let i = 0; i < lists.length; i++) {
        upload(lists[i].url).then(res => {
          let item = this[`fileList_${event.name}`][fileListLen];
          this[`fileList_${event.name}`].splice(fileListLen, 1, Object.assign(item, {
            status: 'success',
            message: '',
            id: 0,
            url: res.data.url
          }))
          this[`${event.name}`] = res.data.url
          fileListLen++
        });
      }
    },

    delete_images: function(event) {
      this[`fileList_${event.name}`].splice(event.index, 1);
      this[`${event.name}`] = '';
    },
  }
}
</script>

<style>
page {
  padding-bottom: 40rpx;
}
.luckform .items {
  margin-top: 30rpx;
}
.luckform .btnbox {
  margin-top: 60rpx;
}
.luckform .btnbox .luckbtn {
  background-color: #3c7bfb;
}

.status_box {
  margin-top: 50rpx;
}
</style>
